import React from 'react';
import {Button, Icon, Row, Col, InputNumber, Checkbox, message} from 'antd';
import {WeaSearchGroup, WeaFormItem, WeaSelect, WeaBrowser, WeaTable, WeaLocaleProvider, WeaTools} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

export default class Tab4Share extends React.Component {

    componentWillMount(){
        this.getYyfqData();
    }

    state = {
        key: 0,
        sharetype: '3',
        sharevalue: '',
        includeSub: false,
        seclevelmin: 0,
        seclevelmax: 100,
        seclevel: '0-100',
        rolelevel: '0',
        jobtitlelevel: '1',
        jobtitlesharevalue: '',
        shareTypeName: '',
        shareValueName: '',
        jobtitlesharevaluename: '',
        objReplaceDatas: [],
        jobReplaceDatas: [],
        selectedRowKeys: [],
        eHasShareRight: this.props.eShare.eHasShareRight,
        eOldShareList: this.props.eShare.eOldShareList || [], // 老版共享数据
        eShareList: this.props.eShare.eShareList || [], // 新版共享数据
        valueFlag:false


    };

    constructor(props) {
        super(props);
        this.getSaveValues = this.getSaveValues.bind(this);
        this.getYyfqData=this.getYyfqData.bind(this);
    }

    render() {
        const {eHasShareRight} = this.state;

        if (eHasShareRight) {
            const {
                sharetype,
                sharevalue,
                shareValueName,
                includeSub,
                seclevelmin,
                seclevelmax,
                rolelevel,
                jobtitlelevel,
                jobtitlesharevalue,
                jobtitlesharevaluename,
                objReplaceDatas,
                jobReplaceDatas,
                selectedRowKeys,
                valueFlag

            } = this.state;
            let objItemVisible = true;
            let seclevelItemVisible = true;
            let rolelevelItemVisible = false;
            let jobtitlelevelItemVisible = false;

            let objItem = '';
            let rolelevelItem = '';
            let jobtitlelevelItem = '';


            if (sharetype === '3') { // 部门
                objItem = (
                    <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@kcil5m`}
                        key={57}
                        title={getLabel(516507, '多部门')}
                        type={57}
                        icon={"icon-coms-hrm"}
                        iconBgcolor={'#217346'}
                        isSingle={false}
                        replaceDatas={
                            sharevalue != '' && shareValueName != '' ?
                                [{
                                    id: sharevalue,
                                    name: shareValueName
                                }] : []}
                        tabs={[
                            {key: '1', name: getLabel(384541, '按组织架构'), selected: false},
                            {key: '2', name: getLabel(130619, '按列表'), selected: false, dataParams: {list: 1},isSearch:true}
                        ]}

                        viewAttr={3}
                        onChange={(ids, names, datas) => this.setState({sharevalue: ids, shareValueName: names})}
                    />
                );
            } else if (sharetype === '2') { // 分部
                objItem = (
                    <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@0c0xka`}
                        key={194}
                        title={getLabel(516508, '多分部')}
                        type={194}
                        icon={"icon-coms-hrm"}
                        iconBgcolor={'#217346'}
                        isSingle={false}
                        tabs={[
                            {key: '1', name: getLabel(384541, '按组织架构'), selected: false},
                            {key: '2', name: getLabel(130619, '按列表'), selected: false, dataParams: {list: 1},isSearch:true}
                        ]}
                        replaceDatas={
                            sharevalue != '' && shareValueName != '' ?
                                [{
                                    id: sharevalue,
                                    name: shareValueName
                                }] : []}
                        viewAttr={3}
                        onChange={(ids, names, datas) => this.setState({sharevalue: ids, shareValueName: names})}
                    />
                );
            } else if (sharetype === '7') { // 安全级别
                objItemVisible = false;
            } else if (sharetype === '1') { // 人力资源
                seclevelItemVisible = false;

                objItem = (
                    <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@nyjlcz`}
                        key={17}
                        title={getLabel(516509, '多人力资源')}
                        type={17}
                        icon={"icon-coms-hrm"}
                        iconBgcolor={'#217346'}
                        isSingle={false}
                        viewAttr={3}
                        replaceDatas={
                            sharevalue != '' && shareValueName != '' ?
                                [{
                                    id: sharevalue,
                                    name: shareValueName
                                }] : []}
                        onChange={(ids, names, datas) => this.setState({sharevalue: ids, shareValueName: names})}
                    />
                );
            } else if (sharetype === '6') { // 角色
                rolelevelItemVisible = true;

                objItem = (
                    <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@5xkvht`}
                        key={267}
                        title={getLabel(122, '角色')}
                        quickSearchName={'rolesmark'}
                        type={267}
                        iconBgcolor={'rgb(179, 46, 55)'}
                        isSingle={true}
                        viewAttr={3}
                        replaceDatas={
                            sharevalue != '' && shareValueName != '' ?
                                [{
                                    id: sharevalue,
                                    name: shareValueName
                                }] : []}
                        onChange={(ids, names, datas) => this.setState({sharevalue: ids, shareValueName: names})}
                    />
                );

                rolelevelItem = (
                    <WeaSelect ecId={`${this && this.props && this.props.ecId || ''}_WeaSelect@bp60bn`}
                        options={[
                            {key: '0', selected: false, showname: getLabel(27511, '部门')},
                            {key: '1', selected: false, showname: getLabel(33553, '分部')},
                            {key: '2', selected: false, showname: getLabel(140, '总部')}
                        ]}
                        value={rolelevel}
                        onChange={value => this.setState({rolelevel: value})}
                    />
                );
            } else if (sharetype === '8') { // 岗位
                seclevelItemVisible = false;
                jobtitlelevelItemVisible = true;

                objItem = (
                    <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@bhxzwq`}
                        key={278}
                        title={getLabel(516510, '多岗位')}
                        type={278}
                        isSingle={false}
                        viewAttr={3}
                        replaceDatas={
                            sharevalue != '' && shareValueName != '' ?
                                [{
                                    id: sharevalue,
                                    name: shareValueName
                                }] : []}
                        onChange={(ids, names, datas) => this.setState({sharevalue: ids, shareValueName: names})}
                    />
                );

                let jobtitlelevelObj = '';
                if (jobtitlelevel == '2') {
                    jobtitlelevelObj = (
                        <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@3hz2yp`}
                            key={57}
                            title={getLabel(516507, '多部门')}
                            type={57}
                            icon={"icon-coms-hrm"}
                            iconBgcolor={'#217346'}
                            isSingle={false}
                            tabs={[
                                {key: '1', name: getLabel(384541, '按组织架构'), selected: false},
                                {key: '2', name: getLabel(130619, '按列表'), selected: false, dataParams: {list: 1},isSearch:true}
                            ]}
                            viewAttr={3}
                            replaceDatas={
                                jobtitlesharevalue != '' && jobtitlesharevaluename != '' ?
                                    [{
                                        id: jobtitlesharevalue,
                                        name: jobtitlesharevaluename
                                    }] : []}
                            onChange={(ids, names, datas) => this.setState({jobtitlesharevalue: ids, jobtitlesharevaluename: names})}
                        />
                    );
                } else if (jobtitlelevel == '3') {
                    jobtitlelevelObj = (
                        <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@e1ibuc`}
                            key={194}
                            title={getLabel(516508, '多分部')}
                            type={194}
                            icon={"icon-coms-hrm"}
                            iconBgcolor={'#217346'}
                            isSingle={false}
                            tabs={[
                                {key: '1', name: getLabel(384541, '按组织架构'), selected: false},
                                {key: '2', name: getLabel(130619, '按列表'), selected: false, dataParams: {list: 1},isSearch:true}
                            ]}
                            replaceDatas={
                                jobtitlesharevalue != '' && jobtitlesharevaluename != '' ?
                                    [{
                                        id: jobtitlesharevalue,
                                        name: jobtitlesharevaluename
                                    }] : []}
                            viewAttr={3}
                            onChange={(ids, names, datas) => this.setState({jobtitlesharevalue: ids, jobtitlesharevaluename: names})}
                        />
                    );
                }

                jobtitlelevelItem = (
                    <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@nti23s`} gutter={16}>
                        <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@lphqov`} span={8}>
                            <WeaSelect ecId={`${this && this.props && this.props.ecId || ''}_WeaSelect@dgl5i0`}
                                options={[
                                    {key: '1', selected: false, showname: getLabel(140, '总部')},
                                    {key: '2', selected: false, showname: getLabel(19438, '指定部门')},
                                    {key: '3', selected: false, showname: getLabel(19437, '指定分部')}
                                ]}
                                value={jobtitlelevel}
                                style={{width: '100px'}}
                                onChange={value => this.setState({jobtitlelevel: value, jobReplaceDatas: [], jobtitlesharevalue: '', jobtitlesharevaluename: ''})}
                            />
                        </Col>
                        <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@ynthl4`} span={16}>
                            {jobtitlelevelObj}
                        </Col>
                    </Row>
                );
            }

            // 记录选中的tab数据行
            const _self = this;
            const rowSelection = {
                onChange(selectedRowKeys) {
                    // if(selectedRowKeys==''){
                    //     $('#shareDel').css("background-color","");
                    // }else{
                    //     $('#shareDel').css("background-color","#55a1f8");
                    // }

                    _self.setState({selectedRowKeys: selectedRowKeys})
                }
            };

            // 表格数据源
            const dataSource = this.state.eOldShareList.concat(this.state.eShareList);

            // 表格列
            const columns = [{
                title: getLabel(21956, '对象类型'),
                dataIndex: 'shareTypeName'
            }, {
                title: getLabel(106, '对象'),
                dataIndex: 'shareValueName'
            }, {
                title: getLabel(683, '安全级别'),
                dataIndex: 'securityLevel'
            }];

            return (
                <div>
                    <WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@4xxhzu`} title={getLabel(81711, '基本信息')} needTigger={true} showGroup={true}>
                        <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@yyktql`} label={getLabel(21956, '对象类型')} labelCol={{span: 6}} wrapperCol={{span: 16}}>
                            {
                                valueFlag==true? <WeaSelect ecId={`${this && this.props && this.props.ecId || ''}_WeaSelect@xr5clw`}
                                    options={[
                                        {key: '3', selected: false, showname: getLabel(27511, '部门')},
                                        {key: '2', selected: false, showname: getLabel(33553, '分部')},

                                        {key: '1', selected: false, showname: getLabel(179, '人力资源')},
                                        {key: '6', selected: false, showname: getLabel(122, '角色')},
                                        {key: '8', selected: false, showname: getLabel(6086, '岗位')}
                                    ]}
                                    value={sharetype}
                                    style={{width: '100px'}}
                                    onChange={value => this.setState({
                                        sharetype: value, objReplaceDatas: [], jobReplaceDatas: [], sharevalue: '', shareValueName: '', jobtitlesharevalue: '', jobtitlesharevaluename: ''
                                    })}
                                />: <WeaSelect ecId={`${this && this.props && this.props.ecId || ''}_WeaSelect@l68xbl`}
                                    options={[
                                        {key: '3', selected: false, showname: getLabel(27511, '部门')},
                                        {key: '2', selected: false, showname: getLabel(33553, '分部')},
                                        {key: '7', selected: false, showname: getLabel(1340, '所有人')},
                                        {key: '1', selected: false, showname: getLabel(179, '人力资源')},
                                        {key: '6', selected: false, showname: getLabel(122, '角色')},
                                        {key: '8', selected: false, showname: getLabel(6086, '岗位')}
                                    ]}
                                    value={sharetype}
                                    style={{width: '100px'}}
                                    onChange={value => this.setState({
                                        sharetype: value, objReplaceDatas: [], jobReplaceDatas: [], sharevalue: '', shareValueName: '', jobtitlesharevalue: '', jobtitlesharevaluename: ''
                                    })}
                                />
                            }

                        </WeaFormItem>
                        {
                            objItemVisible ?
                                <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@afbjam`} label={getLabel(106, '对象')} labelCol={{span: 6}} wrapperCol={{span: 16}}>
                                    {
                                        sharetype === '3' || sharetype === '2' ?
                                            <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@v92907`} gutter={16}>
                                                <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@lhfkxx`} span={16}>
                                                    {objItem}
                                                </Col>
                                                <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@g5e1km`} span={8}>
                                                    <Checkbox ecId={`${this && this.props && this.props.ecId || ''}_Checkbox@a2usol`} checked={includeSub} onChange={e => this.setState({includeSub: e.target.checked})}>{getLabel(125963, '含下级')}</Checkbox>
                                                </Col>
                                            </Row> : objItem
                                    }
                                </WeaFormItem> : ''
                        }
                        {
                            rolelevelItemVisible ?
                                <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@je548p`} label={getLabel(21958, '角色级别')} labelCol={{span: 6}} wrapperCol={{span: 16}}>
                                    {rolelevelItem}
                                </WeaFormItem> : ''
                        }
                        {
                            jobtitlelevelItemVisible ?
                                <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@o656m1`} label={getLabel(28169, '岗位级别')} labelCol={{span: 6}} wrapperCol={{span: 16}}>
                                    {jobtitlelevelItem}
                                </WeaFormItem> : ''
                        }
                        {
                            seclevelItemVisible ?
                                <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@819a02`} label={getLabel(683, '安全级别')} labelCol={{span: 6}} wrapperCol={{span: 16}}>
                                    <InputNumber ecId={`${this && this.props && this.props.ecId || ''}_InputNumber@bp3tp6`} defaultValue={seclevelmin} onChange={value => this.setState({seclevelmin: value})}/>
                                    <span style={{margin: '0 5px'}}>-</span>
                                    <InputNumber ecId={`${this && this.props && this.props.ecId || ''}_InputNumber@k8w7jr`} defaultValue={seclevelmax} onChange={value => this.setState({seclevelmax: value})}/>
                                </WeaFormItem> : ''
                        }
                    </WeaSearchGroup>
                    <WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@m9eqkb`} title={getLabel(1279, '共享信息')} needTigger={true} showGroup={true} customComponent={
                        <div>
                            <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@3xjmhl`} type="primary" className="portal-esetting-wsg-btn"   onClick={this.onAdd.bind(this)}>
                                <Icon ecId={`${this && this.props && this.props.ecId || ''}_Icon@nr1qv3`} type="plus" style={{color: '#fff'}}/>
                            </Button>
                            <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@qnm3fm`} type="primary" disabled={!(selectedRowKeys && selectedRowKeys.length)}   id="shareDel" className="portal-esetting-wsg-btn"  onClick={this.onDelete.bind(this)}>
                                <Icon ecId={`${this && this.props && this.props.ecId || ''}_Icon@u84ca8`} type="minus" style={{color: '#fff'}}/>
                            </Button>
                        </div>
                    }>
                        <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@5lb01i`} rowSelection={rowSelection} columns={columns} dataSource={dataSource} pagination={false}/>
                    </WeaSearchGroup>
                </div>
            );
        } else {
            return (
                <div style={{width: '100%', height: '50px', lineHeight: '50px', borderBottom: '1px solid #e9e9e9', textAlign: 'center'}}>
                    <Icon ecId={`${this && this.props && this.props.ecId || ''}_Icon@feg6oa`} type="exclamation-circle" style={{marginRight: '8px', color: '#fa0'}}/>
                    <span>{getLabel(2012, '对不起，您暂时没有权限！')}</span>
                </div>
            );
        }
    }

    onAdd() {
        const {
            sharetype,
            sharevalue,
            includeSub,
            seclevelmin,
            seclevelmax,
            seclevel,
            rolelevel,
            jobtitlelevel,
            jobtitlesharevalue,
            valueFlag
        } = this.state;
        let {key, shareTypeName, shareValueName, jobtitlesharevaluename, eShareList} = this.state;

        if (sharetype != '7' && (!sharevalue.trim() || (jobtitlelevel != '1' && !jobtitlesharevalue.trim()))) {
            message.warning(getLabel(15859, '必要信息不完整！'));
        } else {
            // 部门(3)、分部(2)、所有人(7)、人力资源(1)、角色(6)、岗位(8)
            if(valueFlag==true){
                shareTypeName = {
                    3: getLabel(27511, '部门'),
                    2: getLabel(33553, '分部'),

                    1: getLabel(179, '人力资源'),
                    6: getLabel(122, '角色'),
                    8: getLabel(6086, '岗位')
                }[sharetype];
            }else{
                shareTypeName = {
                    3: getLabel(27511, '部门'),
                    2: getLabel(33553, '分部'),
                    7: getLabel(1340, '所有人'),
                    1: getLabel(179, '人力资源'),
                    6: getLabel(122, '角色'),
                    8: getLabel(6086, '岗位')
                }[sharetype];
            }

            if ((sharetype == '3' || sharetype == '2') && includeSub) {
                shareValueName += '('+getLabel(125963, '含下级')+')';
            } else if (sharetype == '7') {
                shareValueName = getLabel(1340, '所有人');
            } else if (sharetype == '6') {
                if (rolelevel == 2) {
                    shareValueName += '/' + getLabel(140, '总部');
                } else if (rolelevel == 1) {
                    shareValueName += '/' + getLabel(33553, '分部');
                } else if (rolelevel == 0) {
                    shareValueName += '/' + getLabel(27511, '部门');
                }
            } else if (sharetype == '8') {
                if (jobtitlelevel == 1) {
                    shareValueName += '/' + getLabel(140, '总部');
                } else if (jobtitlelevel == 2) {
                    shareValueName += '/'+getLabel(19438	, '指定部门')+'/' + jobtitlesharevaluename;
                } else if (jobtitlelevel == 3) {
                    shareValueName += '/'+getLabel(19437	, '指定分部')+'/' + jobtitlesharevaluename;
                }
            }

            eShareList.push({
                key: --key,
                shareType: sharetype,
                shareValue: sharevalue,
                includeSub: includeSub,
                roleLevel: rolelevel,
                securityMinLevel: seclevelmin,
                securityMaxLevel: seclevelmax,
                securityLevel: sharetype == '1' || sharetype == '8' ? '' : seclevelmin + '-' + seclevelmax,
                jobShareLevel: jobtitlelevel,
                jobShareValue: jobtitlesharevalue,
                shareTypeName: shareTypeName,
                shareValueName: shareValueName
            });
            this.setState({key: key, eShareList: eShareList});
        }
    }




    onDelete() {
        const {selectedRowKeys, eOldShareList, eShareList} = this.state;

        let isOld = false;
        if (selectedRowKeys && selectedRowKeys.length) {
            const __selectedRowKeys = selectedRowKeys.slice();
            for (let i = 0, len = __selectedRowKeys.length; i < len; i++) {
                selectedRowKeys.splice(i, 1);

                for (let j = 0, len2 = eOldShareList.length; j < len2; j++) {
                    if (eOldShareList[j].key == __selectedRowKeys[i]) {
                        eOldShareList.splice(j, 1);
                        isOld = true;
                        break;
                    }
                }

                if (!isOld) {
                    for (let k = 0, len3 = eShareList.length; k < len3; k++) {
                        if (eShareList[k].key == __selectedRowKeys[i]) {
                            eShareList.splice(k, 1);
                            break;
                        }
                    }
                }
            }
        }

        this.setState({eOldShareList: eOldShareList, eShareList: eShareList});
    }


    getSaveValues() {
        return {
            eOldShareList: this.state.eOldShareList,
            eShareList: this.state.eShareList
        };
    }

    /*获取是否禁用“所有人”权限范围*/
    getYyfqData() {
        WeaTools.callApi('/api/hrm/common/appDetachDisableAll', 'get').then(result=> {
            if(result.status==true){
                this.setState({
                    valueFlag:  result.appDetachDisableAll
                });
            }
        });
    }
}